package tools.shapes;

import java.awt.Graphics;
import java.awt.Color;
import app.DrawingCanvas;
import java.awt.Point;

/**
 * Defines how to draw a Rectangle on the DrawingCanvas
 */
public class RectangleFillShape extends TwoEndShape {

  /* (non-Javadoc)
   * 
   * Defines how to draw the rectangle with corners at the starting and ending
   * point. The smallest coordinates of the starting and ending positions are
   * used to locate the origin of the rectangular shape.  The absolute value
   * of the differences in the x and y coordinates are used for the width and
   * height respectively.
   * 
   * @see tools.shapes.TwoEndShape#draw(java.awt.Graphics, int, int, int, int)
   */
	
	public void addToStack(DrawingCanvas c, Point start, Point end) {
		RectangleFillShape newShape = new RectangleFillShape();
		if (start.x < end.x) {
			newShape.xMin = start.x;
			newShape.xMax = end.x;
		}
		else {
			newShape.xMin = end.x;
			newShape.xMax = start.x;
		}
		if (start.y < end.y) {
			newShape.yMin = start.y;
			newShape.yMax = end.y;
		}
		else {
			newShape.yMin = end.y;
			newShape.yMax = start.y;
		}
		newShape.setColor(c.getpenColor());
		c.push(newShape);
	}
	
	public void draw(DrawingCanvas c) {
		draw(c.getimageBufferGraphics(), xMin, yMin, xMax, yMax);
	}
	
	public void drawOutline(DrawingCanvas c) {
		drawOutline(c.getimageBufferGraphics(), xMin, yMin, xMax, yMax);
	}

	public void resize(Point startPos, Point newPos){

		/* find the corner of interest */
		float a = Math.abs(startPos.x - xMin);
		float b = Math.abs(startPos.x - xMax);
		float c = Math.abs(startPos.y - yMin);
		float d = Math.abs(startPos.y - yMax);
		

		if(a > b){
			if( c > d ){
				xMax = newPos.x;
				yMax = newPos.y;
			}else{ // c < d
				xMax = newPos.x;
				yMin = newPos.y;
			}
		}else{ // a < b
			if( c > d ){
				xMin = newPos.x;
				yMax = newPos.y;
			}else{ // c < d
				xMin = newPos.x;
				yMin = newPos.y;
			}
		}
		
		int temp;
		if (xMax < xMin) {
			temp = xMin;
			xMin = xMax;
			xMax = temp;
		}
		if (yMax < yMin) {
			temp = yMin;
			yMin = yMax;
			yMax = temp;
		}
	}

	public void move(Point startPos, Point newPos){

		/* take difference between the two points and update the coordinates 
		 *   accordingly 
		 **/
		int deltaX = newPos.x - startPos.x;
		int deltaY = newPos.y - startPos.y;

		xMin += deltaX;
		xMax += deltaX;
		yMin += deltaY;
		yMax += deltaY;
	
	}

	
  public void draw(Graphics g, int x0, int y0, int x1, int y1) {
    int shapeX;
    int shapeY;
    int shapeWidth;
    int shapeHeight;
    Color savedColor = g.getColor();
    
    // find smallest x coordinate and calculate width
    if (x0 <= x1) {
      shapeX = x0;
      shapeWidth = (x1-x0)+1;
    }
    else {
      shapeX = x1;
      shapeWidth = (x0-x1)+1;
    }
    
    // find smallest y coordinate and calculate height
    if (y0 <= y1) {
      shapeY = y0;
      shapeHeight = (y1-y0)+1;
    }
    else {
      shapeY = y1;
      shapeHeight = (y0-y1)+1;
    }
    if (this.color != null) {
    	g.setColor(this.color);
    }
    g.fillRect(shapeX, shapeY, shapeWidth, shapeHeight);
    g.setColor(savedColor);
  }
  
  /* (non-Javadoc)
   * 
   * Implemented like draw().  See above.
   * 
   * @see tools.shapes.TwoEndShape#drawOutline(java.awt.Graphics, int, int, int, int)
   */
  public void drawOutline(Graphics g, int x0, int y0,
                                        int x1, int y1) {
    int shapeX;
    int shapeY;
    int shapeWidth;
    int shapeHeight;
    if (x0 <= x1) {
    shapeX = x0;
    shapeWidth = (x1-x0)+1;
    }
    else {
      shapeX = x1;
      shapeWidth = (x0 -x1)+1;
    }
    if (y0 <= y1) {
      shapeY = y0;
      shapeHeight = (y1-y0)+1;
    }
    else {
      shapeY = y1;
      shapeHeight = (y0-y1)+1;
    }
    g.drawRect(shapeX, shapeY, shapeWidth, shapeHeight);
  }
}// end public class RectangleShape extends TwoEndShape
